<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Article;
use App\Models\ArticleType;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ArticleController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Article(['articleType']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('title');
//            $grid->column('type');
            $grid->column("articleType.name","类型");
            $grid->is_recommend()->switch();
            $grid->column('description');
            $grid->column("order_number")->editable()->sortable();
//            $grid->column('content');
            $grid->column('created_at')->sortable();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->like("title");
                $filter->equal('type', "文章类别")
                    ->select(ArticleType::pluck("name", "id")->toArray());
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Article(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->img->image();
//            $show->field('type');
            $show->field('description');
            $show->field('content')->unescape();
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Article(), function (Form $form) {
            $form->display('id');
            $form->text('title')->rules("required",[
                "required"=>"标题必须"
            ]);
            $form->image('img')->chunked()->uniqueName()->autoUpload();
            $form->number("order_number");
            $form->switch("is_recommend");
            $form->select("type", "文章类别")
                ->options(ArticleType::pluck("name", "id")->toArray())
                ->required();
//            $form->text('type');
            $form->text('description')->required();
            $form->editor('content')->rules("required",[
                "required"=>"内容必须"
            ]);

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
